//
//  OtherCollectionViewCell.m
//  礼物说
//
//  Created by dllo on 16/4/23.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "OtherCollectionViewCell.h"

@interface OtherCollectionViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *haiTaoZhongZhuanArr;
@property (nonatomic, assign) NSInteger num;

@end

@implementation OtherCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollection];
    }
    return self;
}

- (void)dealloc {
    
    [_haiTaoZhongZhuanArr release];
    [_haiTaoArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)setHaiTaoArr:(NSMutableArray *)haiTaoArr {
    if (_haiTaoArr != haiTaoArr) {
        [_haiTaoArr release];
        _haiTaoArr = [haiTaoArr retain];
    }
    self.haiTaoZhongZhuanArr = [NSMutableArray arrayWithArray:_haiTaoArr];
}

- (void)createCollection {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake(kWIDTHCELL, kWIDTHS(300));
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:layout];
    _collectionView.pagingEnabled = NO;
    _collectionView.bounces = YES;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[ContentCollectionViewCell class]forCellWithReuseIdentifier:@"contentCell"];
    
    [self createMjbfresh];
}

/** 刷新*/
- (void)createMjbfresh {
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSString * index = [NSString stringWithFormat:@"%ld", (long)_index];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"下拉刷新" object:nil userInfo:@{@"collectionview" : _collectionView , @"index": index}];
    }];
    self.num = 0;
    _collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        
        _num = _num + 20;
        NSString * str = [NSString stringWithFormat:@"%ld", (long)_num];
        NSString * index = [NSString stringWithFormat:@"%ld", (long)_index];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"上拉加载" object:nil userInfo: @{@"num" : str, @"collectionview" : _collectionView, @"index" : index }];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _haiTaoZhongZhuanArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"contentCell" forIndexPath:indexPath];
    cell.model = _haiTaoZhongZhuanArr[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kWIDTHCELL, kHEIGHTS(200));
}

#pragma mark - collection点击方法【详情页】
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GongLvXiangQingViewController * gongLvVc = [[GongLvXiangQingViewController alloc] init];
    ContentModel * model = _haiTaoZhongZhuanArr[indexPath.row];
    
    gongLvVc.number = model.number;
    
    UIViewController * viewcontroller = [self viewController];
    
    [viewcontroller.navigationController pushViewController:gongLvVc animated:YES];
    [gongLvVc release];
    
}


/** 获取父视图*/
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

@end
